#include <stdlib.h>
#include <check.h>
#include <stdio.h>
#include <string.h>
#include <imbus.h>


Suite * memory_suite (void);
Suite * utilities_suite (void);
Suite * base_types_suite (void);
Suite * string_suite (void);

struct __Suite {
	Suite * (* func) ();
	char *name;
	int enable;
};

static struct __Suite g_suites [] = {
	{ memory_suite, "memory", 0 } ,
	{ utilities_suite, "utilities", 0 } ,
	{ base_types_suite, "base-types", 0 },
	{ string_suite, "string", 0 },
	{ NULL, NULL, 0}
};

static int g_all_enable = 0;

static void usage (char *name)
{
	struct __Suite *p;

	printf ("usage: %s\n", name);
	printf ("\t%s suite1 suite2 ...\n", name);
	printf ("\tor %s all\n", name);
	printf ("suites:\n\t");

	for (p = g_suites; p->func != NULL; p++) {
		printf (" %s", p->name);
	}

	printf ("\n");
		
	exit(1);
}

int main(int argc, char **argv)
{
	int nf;
	struct __Suite *p;
	char **arg;

        im_init (&argc, &argv);
	
	if (argc == 1)
		usage(*argv);

	for (arg = argv + 1; *arg != NULL; arg++) {
		if (strcmp (*arg, "all") == 0)
			g_all_enable = 1;
		else { 
			for (p = g_suites; p->func != NULL; p++)
				if (strcmp(p->name, *arg) == 0) 
					p->enable = 1;
		}
	}	

	SRunner *sr = srunner_create (NULL);
		

	for (p = g_suites; p->func != NULL; p++)
		if (p->enable || g_all_enable)
			srunner_add_suite (sr, p->func());

	srunner_set_fork_status (sr, CK_FORK);
	srunner_run_all (sr, CK_NORMAL);
	
	nf = srunner_ntests_failed (sr);

	srunner_free (sr);

	return (nf == 0) ? EXIT_SUCCESS: EXIT_FAILURE;
}
